package com.example.websocket.controller;

import com.example.websocket.pojo.Result;
import com.example.websocket.pojo.dto.FriendDto;
import com.example.websocket.pojo.dto.NoteDto;
import com.example.websocket.pojo.dto.UserListDto;
import com.example.websocket.pojo.po.RequestPo;
import com.example.websocket.pojo.vo.RequestVo;
import com.example.websocket.service.FriendsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ author 韩
 * time: 2024/3/11 18:55
 */
@RestController
@RequestMapping("/friends")
@Tag(name = "FriendsController" ,description = "主要是对朋友这个页面的业务 增删改查，加好友之类的")
public class FriendsController {

    @Autowired
    private FriendsService friendsService;

    @Operation(summary = "获取每个人对应的朋友列表")
    @GetMapping("/getOwnFriends")
    public Result getOwnFriends(String username) {
        List<UserListDto> friends = friendsService.getOwnFriends(username);
//        List<String> list = new ArrayList<>();
//        friends.forEach(value->{
//            if (value.getNote() != null) {
//                list.add(value.getNote());
//            } else {
//                list.add(value.getFriend_name());
//            }
//        });
        return Result.ok(friends);
    }

    @Operation(summary = "修改好友备注")
    @PutMapping("/updateNote")
    public Result updateNote(@RequestBody NoteDto dto) {
        boolean flag = friendsService.updateNote(dto);
        if (flag) {
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @Operation(summary = "搜索朋友")
    @GetMapping("/searchFriend")
    public Result searchFriend(@RequestParam("username") String username,@RequestParam("key")String key) {
        List<UserListDto> friends = friendsService.searchFriend(username,key);
        return Result.ok(friends);
    }
    // 搞一个同意机制 同意之后 才加好友 然后在redis里加信息 ”你好“
    // 添加好友 发post请求
    @Operation(summary = "加好友")
    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody Map<String, String> requestBody, HttpSession session) {
        String user_name = (String) session.getAttribute("username");
        String friend_name = requestBody.get("sender_username");

        FriendDto dto = new FriendDto();
        dto.setFriend_name(friend_name);
        dto.setUser_name(user_name);
        boolean flag  = friendsService.addFriend(dto);
        if (flag) {
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    // 查看是否有这个人
    @Operation(summary = "验证一下好友申请，然后返回信息，不能通过就返回message就是原因，成功的话发一条请求，insert一个请求信息")
    @GetMapping("/verifyFriend")
    public Result verifyFriend(@RequestParam("username") String username, HttpSession session) {
        String flag = friendsService.verifyFriend(username,(String)session.getAttribute("username"));
        return Result.ok(flag);
    }

    //获取好友请求列表
    @Operation(summary = "获取个人的好友列表")
    @GetMapping("/selectRequest")
    public Result selectMyRequest(HttpSession session) {
        String username = (String) session.getAttribute("username");
        List<RequestPo> list = friendsService.selectMyRequest(username);
        List<RequestVo> voList = new ArrayList<>();
        list.forEach(v -> {
            RequestVo requestVo = new RequestVo();
            requestVo.setSender_username(v.getSender_username());
            requestVo.setStatus(v.getStatus());
            voList.add(requestVo);
        });
        System.out.println(voList);
        return Result.ok(voList);
    }

//    @GetMapping("/selectRequest")
//    public Result selectMyRequest(String username) {
//        List<RequestPo> list = friendsService.selectMyRequest(username);
//        List<RequestVo> voList = new ArrayList<>();
//        list.forEach(v -> {
//            RequestVo requestVo = new RequestVo();
//            requestVo.setSender_username(v.getSender_username());
//            requestVo.setStatus(v.getStatus());
//            voList.add(requestVo);
//        });
//        return Result.ok(voList);
//    }

    @Operation(summary = "同意好友请求")
    @PatchMapping("/agreeRequest/{sender_username}")
    public Result agreeRequest(@PathVariable String sender_username,HttpSession session) {
        String receiver_name = (String) session.getAttribute("username");
        Boolean flag = friendsService.agreeRequest(sender_username,receiver_name);
        if (flag) {
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @Operation(summary = "删除一个好友")
    @DeleteMapping("/deleteFriend/{deletedName}")
    public Result deleteFriend(@PathVariable("deletedName") String deletedName,HttpSession session) {
        String username = (String) session.getAttribute("username");
        Boolean flag = friendsService.deletedFriend(deletedName,username);
        if (flag) {
            return Result.ok();
        }else {
            return Result.error("删除失败");
        }
    }
//    @PatchMapping("/agreeRequest/{sender_username}")
//    public Result agreeRequest(@PathVariable String sender_username,String username) {
//        String receiver_name = username;
//        Boolean flag = friendsService.agreeRequest(sender_username,receiver_name);
//        if (flag) {
//            return Result.ok();
//        }else {
//            return Result.error();
//        }
//    }

}
